package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.CaScheduleDto;
import com.dingreading.cloud.soms.entity.CaSchedule;
import com.dingreading.cloud.soms.entity.table.CaScheduleTableDef;
import com.dingreading.cloud.soms.mapper.CaScheduleMapper;
import com.dingreading.cloud.soms.service.CaScheduleService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CaScheduleServiceImpl extends BaseServiceImpl<CaScheduleMapper, CaSchedule, CaScheduleTableDef> implements CaScheduleService {


    @Override
    protected CaScheduleTableDef getTable() {
        return CaScheduleTableDef.caSchedule;
    }

    @Override
    public Page<CaScheduleDto> pageListV3(PageUtil pageUtil, String storeUid, String keyword, Integer status,
                                          Long classId, String className, Long roomId, String username, String projectUid, String teachDate) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (null != status)
            condition.and(table.status.eq(status));
        if (StringUtils.isNotBlank(className))
            condition.and(table.className.like(className));
        if (null != classId)
            condition.and(table.classId.eq(classId));
        if (null != roomId)
            condition.and(table.roomId.eq(roomId));
        if (StringUtils.isNotBlank(projectUid))
            condition.and(table.projectUid.eq(projectUid));
        if (StringUtils.isNotBlank(username))
            condition.and(table.usernames.like("," + username + ","));
        if (StringUtils.isNotBlank(teachDate))
            condition.and(table.teachDates.like("," + teachDate + ","));


        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, CaScheduleDto.class);
    }

    @Override
    public CaSchedule getSchedule(String storeUid, String scheduleUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.scheduleUid.eq(scheduleUid));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public CaScheduleDto getScheduleDto(String storeUid, String scheduleUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.scheduleUid.eq(scheduleUid));
        QueryWrapper wrapper = QueryWrapper.create().where(condition);
        return getMapper().selectOneByQueryAs(wrapper, CaScheduleDto.class);
    }

    @Override
    public int dropBean(String storeUid, String scheduleUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.scheduleUid.eq(scheduleUid));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public boolean addUseLessonNum(String storeUid, String scheduleUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.scheduleUid.eq(scheduleUid));
        return UpdateChain.of(CaSchedule.class)
                .set(table.useLessonNum, table.useLessonNum.add(1))
                .where(condition)
                .update();
    }

    @Override
    public boolean updateStudentNum(String storeUid, Long classId, int stuNum) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.classId.eq(classId))
                .and(table.status.eq(1))
                .and(table.useLessonNum.eq(0));
        return UpdateChain.of(CaSchedule.class)
                .set(table.studentNum, stuNum)
                .where(condition)
                .update();
    }

    @Override
    public boolean updateLessonNum(String storeUid, String scheduleUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.scheduleUid.eq(scheduleUid));
        return UpdateChain.of(CaSchedule.class)
                .set(table.lessonNum, table.lessonNum.add(-1))
                .where(condition)
                .update();
    }

}
